What is @cucumber/message-streams?
@cucumber/message-streams is a package that provides utilities for working with Cucumber messages in a streaming fashion. It allows you to read, write, and transform Cucumber messages using streams, which can be useful for processing large sets of test results or integrating with other tools.
What are @cucumber/message-streams's main functionalities?
Reading Cucumber Messages from a Stream
This feature allows you to read Cucumber messages from a NDJSON (Newline Delimited JSON) stream. The code sample demonstrates how to create a readable stream from a file and pipe it through the NdjsonToMessageStream to process each message.
const { NdjsonToMessageStream } = require('@cucumber/message-streams');
const fs = require('fs');
const inputStream = fs.createReadStream('cucumber.ndjson');
const messageStream = new NdjsonToMessageStream();
inputStream.pipe(messageStream).on('data', (message) => {
console.log(message);
});
Writing Cucumber Messages to a Stream
This feature allows you to write Cucumber messages to a NDJSON stream. The code sample demonstrates how to create a writable stream to a file and use the MessageToNdjsonStream to write a Cucumber message to the file.
const { MessageToNdjsonStream } = require('@cucumber/message-streams');
const fs = require('fs');
const outputStream = fs.createWriteStream('output.ndjson');
const messageStream = new MessageToNdjsonStream();
messageStream.pipe(outputStream);
messageStream.write({
type: 'TestRunStarted',
timestamp: new Date().toISOString()
});
messageStream.end();
Transforming Cucumber Messages
This feature allows you to transform Cucumber messages as they are read from a stream and written to another stream. The code sample demonstrates how to create a transform stream that adds a custom property to each message and then pipes the messages through the transformation.
const { Transform } = require('stream');
const { NdjsonToMessageStream, MessageToNdjsonStream } = require('@cucumber/message-streams');
const fs = require('fs');
const inputStream = fs.createReadStream('cucumber.ndjson');
const outputStream = fs.createWriteStream('transformed.ndjson');
const transformStream = new Transform({
objectMode: true,
transform(message, encoding, callback) {
// Example transformation: add a custom property
message.customProperty = 'customValue';
callback(null, message);
}
});
inputStream
.pipe(new NdjsonToMessageStream())
.pipe(transformStream)
.pipe(new MessageToNdjsonStream())
.pipe(outputStream);
Other packages similar to @cucumber/message-streams
stream-json
stream-json is a package for processing large JSON files in a streaming fashion. It provides tools for parsing, transforming, and stringifying JSON data using streams. Compared to @cucumber/message-streams, stream-json is more general-purpose and not specifically tailored to Cucumber messages.
JSONStream
JSONStream is a package that provides streaming JSON parsing and stringifying. It is useful for working with large JSON datasets and can be used to process JSON data in a memory-efficient way. Like stream-json, JSONStream is a general-purpose tool and does not have specific support for Cucumber messages.
event-stream
event-stream is a toolkit for working with streams in Node.js. It provides a variety of utilities for creating, transforming, and consuming streams. While it is not specifically designed for JSON or Cucumber messages, it can be used in conjunction with other packages to achieve similar functionality.